package com.cssw.fyzb.busi.api.turntable;

import com.cssw.fyzb.busi.api.user.ListFans;
import com.cssw.fyzb.busi.convert.Converter;
import com.cssw.fyzb.busi.service.turntable.TurntableService;
import com.cssw.fyzb.core.model.dodb.Turntable;
import com.cssw.fyzb.core.model.dodb.TurntableLog;
import com.cssw.fyzb.core.model.vo.turntable.TurntableLogVo;
import com.cssw.fyzb.core.model.vo.turntable.TurntableVo;
import com.cssw.fyzb.core.model.vo.user.PublicUserInfoVo;
import com.cssw.fyzb.busi.service.user.UserRelService;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.core.model.dodb.User;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.*;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取转盘礼物列表
 *
 * @author wondream
 */
@Function(description = "获取转盘礼物列表", parameters = {
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = true),
}, result = @Result(
        properties = {
                @Property(name = "total", type = Type.Integer, description = "总页数"),
                @Property(name = "count", type = Type.Integer, description = "总行数"),

        },
        listProperties = {
                @ListProperty(name = "list", properties = {
                        @Property(name = "id", type = Type.Integer, description = "礼物编号"),
                        @Property(name = "type", type = Type.Integer, description = "礼物类型1福币2礼物"),
                        @Property(name = "typeVal", type = Type.String, description = "类型值"),
                        @Property(name = "thumb", type = Type.String, description = "图片"),
                        @Property(name = "uptime", type = Type.String, description = "时间"),
                })
        }
))

@Component
public class GetTurntableLog implements Action {

    @Autowired
    private TurntableService turntableService;

    private static final Logger logger = LoggerFactory.getLogger(GetTurntableLog.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        try {
            Page<TurntableLog> page = turntableService.getTurntableLog(userId);

            List<TurntableLogVo> list = ((Converter<List<TurntableLog>, List<TurntableLogVo>>) logs -> {
                List<TurntableLogVo> vos = new ArrayList<>();
                logs.forEach((v) -> {
                    TurntableLogVo vo = new TurntableLogVo();
                    vo.setLogId(v.getLogId());
                    vo.setUserId(v.getUserId());
                    vo.setTurntableId(v.getTurntableId());
                    vo.setPrice(v.getPrice());
                    vo.setTurntableName(v.getTurntableName());
                    vo.setPayType(v.getPayType());
                    vo.setWin(v.getWin());
                    vo.setDayTime(v.getDayTime());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String createTime = sdf.format(v.getCreateTime());
                    vo.setCreateTime(createTime);
                    vos.add(vo);
                });
                return vos;
            }).convert(page.getList());
            return Response.success().put("list", list).put("total", page.getTotalPage()).put("count", page.getTotalRow());
        } catch (Exception e) {
            logger.info("", e);
            return Response.fail("操作异常");
        }
    }

}

